<!doctype html>
<html lang="en">
 <head>
	<meta charset="UTF-8">
	<title>3.8.1 Alarm history check</title>
	<link rel="stylesheet" href="../css/index.css" />
	<link rel="stylesheet" href="../css/index-ui.css" />
	<link rel="stylesheet" href="../css/easyui.css">
	<link rel="stylesheet" href="../css/icon.css">
	<link rel="stylesheet" href="../css/demo.css">
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
  <script type="text/javascript" src="../js/webapi.js"></script>
  <script type="text/javascript" src="../js/ams.util.js"></script>
  <script type="text/javascript" src="../js2/loginCookies.js"></script>
 </head>
 <body onload="page_init()">	
	<!-- 搜索 -->
	<div class="alarm-page-search">
		<ul>
			<li>
				<span>ID：</span>
				<input class="easyui-numberbox" id="nAlmId" type="text" placeholder="alarmID"/>
			</li>
			<li>
					<tr>
						<span>time：</span>
						<td>
							<input class="easyui-datebox" id="sStartDay" data-options="sharedCalendar:'#cc'">
						</td>
						<td>&nbsp;~&nbsp;</td>
						<td>
							<input class="easyui-datebox" id="sEndDay" data-options="sharedCalendar:'#cc'">
						</td>
					</tr>
				<div id="cc" class="easyui-calendar"></div>
			</li>
			<li>
				<span>Level：</span>
				<select class="easyui-combobox" id="nLevel" name="state" style="width:100px;" panelHeight="auto">
					<option value="-1">All</option>
					<option value="0">Inform</option>
					<option value="1">Warn</option>
					<option value="2">Alarm</option>
					<option value="3">Intlks</option>
				</select>
			</li>
			<li>
        <tr>
          <td>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:70px;margin:0 10px 0 10px;" onclick="onQuery()">Query</a>
          </td>
        </tr>
        <tr>
          <td>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:''" style="width:70px;margin:0 10px 0 10px;" onclick="onReport()">Export</a>
          </td>
        </tr>
			</li>
			<div class="clearfix"></div>
		</ul>
	</div>
	<!-- 表格 easyui-datagrid  data-options="method:'get',singleSelect:true,pagination:true" -->
	<div class="alarm-table">
		<table id="alarmlogs" class="easyui-datagrid" title="" style="width:100%;height:620px"
			data-options="ctrlSelect:true,singleSelect:true,autoRowHeight:'true'" pagination="false">
			<thead>
				<tr>
					<th data-options="field:'id',width:50">ID</th>
					<th data-options="field:'enableTime',width:135">startTime</th>
					<th data-options="field:'clearTime',width:135">endTime</th>
					<th data-options="field:'level',width:60">Level</th>
					<th data-options="field:'equipId',hidden:true">EquipId</th>
					<th data-options="field:'desc'" style="width:620px;">Desc</th>
				</tr>
			</thead>
		</table>
		<div id="pp" style="background:#efefef;border:1px solid #ccc;"></div>
		<script>
		var eqn = null;
		var page_Number = null;
		var page_Size = null;
		function page_init(){
			if (eqn == null)
				eqn = get_page_param('eq');
			
			$('#alarmlogs').datagrid('options').pageSize = 30;
			$('#pp').pagination({
				pageSize:30,
				layout:['list','prev','manual','next']
		});
		Search();
		}
			var count =1;
			$(function () {
		     $("#alarmlogs").datagrid({
		         onDblClickRow: function (index, row) {
			         	var row = $('#alarmlogs').datagrid('getSelected');
			         	$.ajax({
								type: 'POST',
								contentType: 'application/json',
								url: getWebServiceUrl("getEqAlarmOpTip"),
								data:JSON.stringify({
									"eqName":eqn,
									"almId":row.id
								}),
								dataType: 'json',
								success: function (response) {
									console.log(JSON.parse(response.d))
									var icon = "info";
				         	if (row.level == "Inform"){
				         		icon = "info";
				         	}
				         	else if (row.level == "Warn"){
				         		icon = "warning";
				         	}
				         	else{
				         		icon = "error";
				         	}
				         	var jsonData = JSON.parse(response.d);
				         	var title = jsonData.title;
				         	var info = "";
				         	for (var i = 0; i < jsonData.rows.length; i++){
				         		if (i > 0){
				         			info += "<br>";
				         		}        		
				         		info += jsonData.rows[i].info;
				         	}
				         	$.messager.alert(title, info, icon);
								},
								error: function (error) {
									$.messager.alert("Error", "Error: " + error, "error");
								}
							});
		         }
		     });
			 })
			 function pagerFilter(data){
				if (typeof data.length == 'number' && typeof data.splice == 'function'){
					data = {
						total: data.length,
						rows: data
					}
				}
				var dg = $(this);
				var opts = dg.datagrid('options');
				var pager = dg.datagrid('getPager');
				pager.pagination({
					onSelectPage:function(pageNum, pageSize){
						opts.pageNumber = pageNum;
						opts.pageSize = pageSize;
						pager.pagination('refresh',{
							pageNumber:pageNum,
							pageSize:pageSize
						});
						dg.datagrid('loadData',data);
					}
				});
				if (!data.originalRows){
					data.originalRows = (data.rows);
				}
				var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
				var end = start + parseInt(opts.pageSize);
				data.rows = (data.originalRows.slice(start, end));
				return data;
			}
			function Search() {
				if (ams_is_sim_mode()) {
					$('#alarmlogs').datagrid({loadFilter:pagerFilter}).datagrid('loadData', getData());
					return;
				}
				
				$('#alarmlogs').datagrid({
				//rownumbers:'true',
				//showHeader:false,
				singleSelect:'true',
				lines:true,
				clickToEdit: false,
				dblclickToEdit: false,
				loadMsg: 'Loading Data ......',
				method:'get'
				});
				
				var eqn = get_page_param('eq');
				//pageNumber为datagrid的当前页码
				var page_Number = $('#alarmlogs').datagrid('options').pageNumber;
				//pageSize为datagrid的每页记录条数
				var page_Size = $('#alarmlogs').datagrid('options').pageSize;
				// var path = $("#fromGrid").attr("action");			
				$.ajax({
					type: 'POST',
					contentType: 'application/json',
					url: getWebServiceUrl("GetEquipmentAlarms"),
					data:JSON.stringify({"equipId":eqn,"sStartDay":"","sEndDay":"","nLevel":-1,"nIndex":0,"nAlmId":-1,"nCount":page_Size}),
					dataType: 'json',
					success : function(datas) {
						// console.log(getData())
						console.log(JSON.parse(datas.d))
						$('#alarmlogs').datagrid('loadData',JSON.parse(datas.d));
						if(JSON.parse(datas.d).length == page_Size){
                count += page_Size;
                $('#pp').pagination({
								total:count
							});
						}
						console.log(count)
					},
					error: function (error) {
						//alert(errorThrown.message);
						//console.log(error)
						$.messager.alert("Error", "Error: " + error, "error");
					}
				});
			}
			
			function onQuery(){
				var nAlmId = $("#nAlmId").val();
				if(nAlmId == ''){
	         nAlmId = -1;
				}
				console.log(nAlmId)
				if (ams_is_sim_mode()) {
					
				} else{
					page_Number = $('#alarmlogs').datagrid('options').pageNumber;
					//pageSize为datagrid的每页记录条数
					if (page_Size == null)
						page_Size = $('#alarmlogs').datagrid('options').pageSize;
						
					$.ajax({
						type: 'POST',
						contentType: 'application/json',
						url: getWebServiceUrl("GetEquipmentAlarms"),
						data:JSON.stringify({
							"equipId":eqn,
							"sStartDay":$("#sStartDay").val(),
							"sEndDay":$("#sEndDay").val(),
							"nLevel":$("#nLevel").val(),
							"nIndex":0,
							"nAlmId":nAlmId,
							"nCount":page_Size
						}),
						dataType: 'json',
						success: function (response) {
							//console.log(JSON.parse(response.d))
							$('#alarmlogs').datagrid('loadData',JSON.parse(response.d));
							if(JSON.parse(response.d).length == page_Size){
	                count += page_Size;
	                $('#pp').pagination('refresh',{
										total: count,
										pageNumber: 1
									});
							}
						},
						error: function (error) {
							//alert(errorThrown.message);
							//console.log(error) 
							$.messager.alert("Error", "Error: " + error, "error");
						}
					});
				}
		  }
	  
			$('#pp').pagination({
			    pageSize:30,
			    afterPageText:'',
			    displayMsg:'',
			    onSelectPage:function(pageNumber, pageSize){
			    	$(this).pagination('loading');
				    var almId = $("#nAlmId").val() == '' ? -1 : $("#nAlmId").val();
				    page_Number = pageNumber;
				    page_Size = pageSize;
						$.ajax({
							url : getWebServiceUrl("GetEquipmentAlarms"),
							type : "post",
							contentType: 'application/json',
							data:JSON.stringify({
								"equipId" : eqn,
								"sStartDay" : $("#sStartDay").val(),
								"sEndDay" : $("#sEndDay").val(),
								"nLevel" : $("#nLevel").val(),
								"nIndex" : (pageNumber - 1) * pageSize,
								"nCount" : pageSize,
								"nAlmId" : almId
							}),
							dataType : "json",
							success : function(datas) {
								console.log(JSON.parse(datas.d).length)
								console.log(count)
								$('#alarmlogs').datagrid('loadData',JSON.parse(datas.d));
								if((JSON.parse(datas.d).length == pageSize) && (pageNumber*pageSize) > count){
		                            count += pageSize;
		                            $('#pp').pagination({
																total:count
									});
								}
							},
							error: function (error) {
								//alert(errorThrown.message);
								//console.log(error) 
								$.messager.alert("Error", "Error: " + error, "error");
							}
						});
					$(this).pagination('loaded');
				}
			});

			function onReport(){
				var nAlmId = $("#nAlmId").val();
				if(nAlmId == ''){
	         nAlmId = -1;
				}
				
				if (ams_is_sim_mode()) {
					
				} else{
					page_Number = $('#alarmlogs').datagrid('options').pageNumber;
					//pageSize为datagrid的每页记录条数
					page_Size = $('#alarmlogs').datagrid('options').pageSize;

					$.ajax({
						type: 'POST',
						contentType: 'application/json',
						url: getWebServiceUrl("GetEquipAlarmsReport"),
						data:JSON.stringify({
							"equipId":eqn,
							"sStartDay":$("#sStartDay").val(),
							"sEndDay":$("#sEndDay").val(),
							"nAlmId":nAlmId,
							"nLevel":$("#nLevel").val()
						}),
						dataType: 'json',
						success: function (response) {
							var fileName = JSON.parse(response.d).name;
							if (fileName != ""){
								parent.location.href = 'http://127.0.0.1:8080/Report/' + JSON.parse(response.d).name + ".csv"
							}
							else{
								$.messager.alert("Error", "Error: Failed to create report.", "error");
							}
						},
						error: function (error) {
							//alert(errorThrown.message);
							//console.log(error) 
							$.messager.alert("Error", "Error: " + error, "error");
						}
					});
				}
		  }
			
			function getData(){
				var rows = [];
				for(var i=1; i<=80; i++){
					var amount = Math.floor(Math.random()*1000);
					var price = Math.floor(Math.random()*1000);
					rows.push({
						id: i,
						enableTime: '2018-10-21 15:21:54',
						clearTime: '2018-10-21 15:23:35',
						level: 'Alarm',
						equipId: price,
						desc: 'alarm desc',
						//alarmContent: 'Note '+i
					});
				}
				return rows;
			}

			$.fn.datebox.defaults.formatter = function(date){
				var y = date.getFullYear();
				var m = date.getMonth()+1;
				var d = date.getDate();
				return y+'-'+m+'-'+d;
			};
			
			$.fn.datebox.defaults.parser = function(s){
				var t = Date.parse(s);
				if (!isNaN(t)){
					return new Date(t);
				} else {
					return new Date();
				}
			}
		</script>
	</div>
 </body>
</html>